package dk.isten.andro.main;

import java.io.IOException;
import java.util.ArrayList;

import util.AarupbkXMLFeeds;
import util.Item;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import dk.isten.andro.R;


public class Nyheder_Akt extends SuperActivity implements OnItemClickListener, OnClickListener {
    /** Called when the activity is first created. */
   ImageView roed, horn,gul, imageView1;
   MediaPlayer enLyd;
   Item item;
   public static ArrayList<util.Item> news;
   public boolean isInFront;
   AarupbkXMLFeeds feed = new AarupbkXMLFeeds();

	public void onResume() {
		super.onResume();
	    isInFront = true;
	    registerReceiver(nyhederOpdateret, new IntentFilter(BaseApp.OpdaterStilling.OPDATERINGSINTENT_Stilling));
	}

	public void onPause() {
		super.onPause();
	    isInFront = false;
	    unregisterReceiver(nyhederOpdateret);
	}
   

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stadionapp);
        

//		opdaterNyheder();
		
        //sæt listview til det rigtig layout. igen
		ListView listView_news = (ListView) findViewById(R.id.ListView_news);
		listView_news.setOnItemClickListener(this);

		
		news = feed.getNews();
		Log.d("er news null?", news.size() + "");
		//        AarupbkXMLFeeds feed = new AarupbkXMLFeeds();
		if(news.size()==1){
			Log.d("nyheder_akt","news.size=1");
			opdaterNyheder();
//			try {
//			Log.d("getData fra baseapp", "succes");
////			InputStream is = getResources().openRawResource(R.raw.rssnews);
//			news.clear();
//			news = feed.getData(getResources().openRawResource(R.raw.rssnews), this);
//			Log.d("efter getdata er kaldt igen",news.get(0).getCategory()+ news.get(0).getTitle());
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	}

		listView_news.setAdapter(new ArrayAdapter(this, R.layout.listeelement_news, R.id.listeelem_title, news) {
			
			@Override
			public View getView(int position, View cachedView, ViewGroup parent) {
				View view = super.getView(position, cachedView, parent);
				
				TextView listeelem_overskrift = (TextView) view.findViewById(R.id.listeelem_title); 	//sæt layout for TextView
				listeelem_overskrift.setText(Html.fromHtml( news.get(position).getTitle())); 							//SetText fra ArrayList
				TextView listeelem_beskrivelse = (TextView) view.findViewById(R.id.listeelem_description);
				listeelem_beskrivelse.setText(Html.fromHtml( news.get(position).getCategory())); 
				
				return view;
			}
		});
		
        gul = (ImageView) findViewById(R.id.yellow);
        horn = (ImageView) findViewById(R.id.horn);
        roed = (ImageView) findViewById(R.id.red);
        imageView1 = (ImageView) findViewById(R.id.imageView1);
		
		imageView1.setOnClickListener(this);
        gul.setOnClickListener(this);
        horn.setOnClickListener(this);
        roed.setOnClickListener(this);
        
	}
	
	private void opdaterNyheder() {
	
			try {
				AarupbkXMLFeeds feed = new AarupbkXMLFeeds();
				Log.d("getData fra baseapp1", "succes");
//				InputStream is = getResources().openRawResource(R.raw.rssnews);
				news.clear();
				news = feed.getData(getResources().openRawResource(R.raw.rssnews), this);
				Log.d("efter getdata er kaldt igen",news.get(0).getCategory()+ news.get(0).getTitle());
			} catch (IOException e) {
				e.printStackTrace();
			}
		
		
	}

	@Override
	public void onClick(View v) {

if(v==imageView1){
            
        	Intent i = new Intent(this, FrontActivity.class);
           
                    startActivity(i);
                    
        }else if(v==gul){
                
                	Intent i = new Intent(this, yellow.class);
                   
                            startActivity(i);
                            
                }else if(v==roed){
                	Intent i = new Intent(this, red.class);
                    
                    		startActivity(i);
                }else if(v==horn){
                	
                    setVolumeControlStream(AudioManager.STREAM_MUSIC);
                    AudioManager audiomanager = (AudioManager) this.getSystemService(AUDIO_SERVICE);
                    //skru op for lyden
                    int  maxLydStyrke = audiomanager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); 

                    audiomanager.setStreamVolume(AudioManager.STREAM_MUSIC, maxLydStyrke, AudioManager.FLAG_SHOW_UI);
                    
                    
                	MediaPlayer enLyd = MediaPlayer.create(this, R.raw.vuvuzela);
                    enLyd.setVolume(9, 9);
                    enLyd.start();
        }
                }

	//start en ny activity med en enkelt spiller
		public void onItemClick(AdapterView<?> l, View v, int position, long id) {

			Intent i = new Intent(this, Full_News_Akt.class);
//			send information om position med intent
			i.putExtra("position", position);
			startActivity(i);
		}
		
		private BroadcastReceiver nyhederOpdateret = new BroadcastReceiver() {
			@Override
			public void onReceive(Context ctx, Intent i) {
				Log.d("broadcastreciever", "NyhederOpdateret");
				opdaterNyheder();
			}
		};
		
}